<template>
    <div class="post" v-if="post">
      <div>{{post.title}}</div>
       <div class="link" v-if="shop" @click="onPostList">
        <img :src="shop.user.avatar">
        <span>{{shop.user.nickname}}的种草</span>
      </div>
      <div class="ops">
        <span class="time">{{post.time}} 
          <span class="block">发布</span>
        </span>
        <div class="m-btns" v-if="me && me.l_1 === 1 && me.invite_code === post.shop_name">
          <span @click="onRemove">删除</span>
          <span v-if="post.sort === 0" @click="setTop">置顶</span>
          <span v-if="post.sort > 0" @click="cancelTop">取消置顶</span>
        </div>
      </div>
      <div class="detail" v-for="(item, index) in post.content" :key="index">
          <image :src="item.image + '?imageMogr2/auto-orient/thumbnail/640x/quality/75'" mode="widthFix"/>
          <rich-text :nodes="item.text"></rich-text>
      </div>
      <div class="p-list" v-if="post.products&& post.products.length> 0">
        <div class="cap">推荐商品</div>
        <div v-for="(item, index) in post.products" :key="index" @click="toProDetail(item.id)">
          <div class="item clearfix">
            <img :src="item.image + '?imageView2/1/w/120/h/120/q/75'">
            <div class="info">
              <span class="title">{{item.title}}</span>
              <span class="price">￥{{item.price}}</span>
            </div>
          </div>
        </div>
      </div>
      <div class="back">
        <div class="item" @click="setLike">
          <van-icon name="like" v-if="post.liked"   class="like-icon liked"/>
          <van-icon name="like-o" v-if="!post.liked"  class="like-icon" />
          <span class="title liked" v-if="post.liked">{{post.like_num}}</span>
          <span class="title" v-if="!post.liked">{{post.like_num}}</span>
          <span class="title" v-if="!post.like_num">赞一下</span>
        </div>
         <div class="item" style="color:#ddd">
           |
        </div>
        <div class="item" style="text-align:left; flex:1.3" @click="toHome">
          <van-icon name="wap-home" class="like-icon" />
          <span class="title">返回首页</span>
        </div>
      </div>
    <van-toast id="van-toast"/>
    <van-dialog id="van-dialog"/>
    </div>
</template>

<script>
import * as PostAction from '@/api/post'
import * as ShopAction from '@/api/shop'
import * as Utils from '@/utils'
import { mapState } from 'vuex'
import Toast from 'vant-weapp/dist/toast/toast'
import Dialog from 'vant-weapp/dist/dialog/dialog'

export default {
  data () {
    return {
      post: null,
      shop: null
    }
  },
  computed: {
    ...mapState(['me'])
  },
  onLoad (query) {
    wx.showNavigationBarLoading()
    this.post = null
    this.shop = null
    // scene存在,则优先判断
    if (query.scene) {
      let params = Utils.default.parseQuery(decodeURIComponent(query.scene))
      if (params.name) this.fetchShop(params.name)
      if (params.id) this.fetchPost(params.id)
      if (params.ic && params.ic.length === 6) wx.setStorageSync('__inviteCode', params.ic)
      return
    }
    if (query.name) this.fetchShop(query.name)
    if (query.code) wx.setStorageSync('__inviteCode', query.code)
    if (query.id) this.fetchPost()
  },
  onShareAppMessage (res) {
    return {
      title: this.shareTitle(),
      imageUrl: this.post.content[0].image,
      path: this.sharePath()
    }
  },
  onPullDownRefresh () {
    wx.showNavigationBarLoading()
    this.fetchPost()
    wx.stopPullDownRefresh()
  },
  methods: {
    toHome () {
      wx.switchTab({ url: '../home/index' })
    },
    shareTitle () {
      if (!this.shop) return
      if (!this.me) return `${this.title}`
      if (this.me) return `${this.me.nickname}向您推荐了${this.post.title}`
    },
    sharePath () {
      if (!this.shop) return
      let url = '/pages/post/detail?id=' + this.post.id + '&name=' + this.shop.name
      let code = ''
      if (this.me && this.me.invite_code) {
        code = this.me.invite_code
      } else {
        code = this.shop.name
      }
      url = url + '&code=' + code
      return url
    },
    toProDetail (id) {
      wx.navigateTo({ url: '../product/detail?id=' + id })
    },
    onPostList () {
      wx.navigateTo({ url: '../shop/index?name=' + this.shop.name })
    },
    fetchShop (name) {
      ShopAction.show(name).then(data => {
        this.shop = data
        this.$store.commit('set_shop_name', data.name)
      }).catch(err => Toast(err.msg))
    },
    setLike () {
      if (!this.me) {
        this.toLogin()
        return
      }
      if (this.post.liked) {
        this.cancelLike()
        return
      }
      this.$set(this.post, 'liked', true)
      this.$set(this.post, 'like_num', this.post.like_num + 1)
      PostAction.likePost(this.post.id).then(() => {
      }).catch(err => Toast(err.msg))
    },
    cancelLike () {
      if (!this.me) {
        this.toLogin()
        return
      }
      this.$set(this.post, 'liked', false)
      this.$set(this.post, 'like_num', this.post.like_num - 1)
      PostAction.cancelPost(this.post.id).then(data => {
      }).catch(err => Toast(err.msg))
    },
    toLogin () {
      Dialog.confirm({
        title: '美控家',
        message: '您还没有登录，是否现在前往登录'
      }).then(() => {
        wx.redirectTo({ url: '../auth/login-index?target=post-detail&id=' + this.post.id })
      }).catch(() => {})
    },
    setTop () {
      PostAction.setTop(this.post.id)
        .then(data => {
          Toast('置顶成功')
          this.post = data
        })
        .catch(err => Toast(err.msg))
    },
    cancelTop () {
      PostAction.cancelTop(this.post.id)
        .then(data => {
          Toast('取消置顶成功')
          this.post = data
        })
        .catch(err => Toast(err.msg))
    },
    onRemove () {
      Dialog.confirm({
        title: '提示',
        message: '请问是否删除当前种草'
      }).then(() => {
        PostAction.deletePost(this.post.id)
          .then(data => {
            Toast('删除成功')
            wx.switchTab({ url: '../home/index' })
          })
          .catch(err => Toast(err.msg))
      }).catch(() => {})
    },
    fetchPost () {
      PostAction.show(this.$root.$mp.query.id)
        .then(data => {
          this.post = data
          this.post.time = Utils.default.getDateDiffCountdown(this.post.created_at)
          wx.hideNavigationBarLoading()
        })
        .catch(err => {
          wx.hideNavigationBarLoading()
          Toast(err.msg)
        })
    }
  }
}
</script>

<style lang="less" scoped>
.top-nav{
  .van-icon{
    color: #d76b81;
  }
}
  .back {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    color: #6A6A6A;
    background: #FAFAFA;
    display: flex;
    justify-content: space-around;
    padding:15px;
    font-size: 28rpx;
    .like {
      display: inline-block;
      padding: 0;
    }
     .like-icon {
      vertical-align:middle;
      font-size:15px;
    }
    .title {
      vertical-align:top;
      font-size:14px;
      margin:0 5px;
    }
    .item {
      flex: 1;
      text-align: center;
    }
  }
  .like {
      padding:10rpx 20rpx;
      padding-left:0;
      display:inline-block;
    }
    .like-icon {
      vertical-align: middle;
    }
    .liked {
    color: #e4668d;
  }
.post {
  padding: 15px;
  position: relative;
  padding-bottom: 120rpx;
  .link {
    display: flex;
    flex-flow: row nowrap;
    justify-items: center;
    margin: 30rpx 0;
    span {
      color:#575757;
      line-height:55rpx;
      margin:0 5px;
      font-size: 26rpx;
      font-weight:500;
    }
    img {
      width:50rpx;
      height:50rpx;
      border-radius:50%;
    }
  }
  .ops{
    margin-bottom: 10px;
    font-size: 13px;
    color: #7E7E7E;
    .time {
      padding: 10rpx;
      color: #535353;
      .block {
        color: #999;
      }
    }
    .m-btns {
      padding: 10rpx;
      display:inlne-block;
      float:right;
      span{
        margin-left: 10px;
      }
    }
  }
  .detail {
    margin: 10px 0;
    color: #333333;
    display: block;
    font-size: 13px;
    letter-spacing: 1px;
    image {
      width: 100%;
    }
  }
  .p-list{
    margin-top: 20px;
     .cap {
      font-size: 14px;
      margin-bottom: 10px;
    }
    .item{
      background: #f5f5f5;
      padding: 8px;
      margin-bottom: 5px;
      height: 120rpx;
      overflow: hidden;
    }
    img{
      width: 120rpx;
      height: 120rpx;
      display: block;
      float: left;
      margin-right: 10px;
    }
    .info{
      height: 120rpx;
      overflow: hidden;
      .title{
        font-size:26rpx;
        color:#555;
        display:block;
        height:66rpx;
        overflow:hidden;
      }
      .price{
        font-size: 13px;
        color: #d76b81;
      }
    }
  }

}
</style>
